#include <bits/stdc++.h>

using namespace std;

struct LCT
{

int main()
{
	scanf("%d",&n);
	for(i=1;i<=n;++i)scanf("%d",&a[i]);
	scanf("%d",&m);
	for(i=1;i<=m;++i)
	{
		scanf("%s",op);
		if(op[0]=='Q')
		{
			scanf("%d",&x);
			printf("%d\n",S.get_root(x));
		}
		else
		{
			scanf("%d%d",&x,&y);
			if(!y)S.cut(x); S.link(x,y);
		}
	}
	return 0;
}
